// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.ast.*;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.text.SimpleDateFormat;
// import java.util.Collections;
// import java.util.Date;
// import java.util.List;
// import java.util.TimeZone;

#include "SQLDateTimeExpr.h"
#include "../SQLDataTypeImpl.h"
#include "../../../DbType.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/instanceof.h"
#include "SQLCharExpr.h"
#include "../SQLExpr.h"

SQLDataType_ptr SQLDateTimeExpr::DATA_TYPE = SQLDataType_ptr(new SQLDataTypeImpl(make_string_ptr("datetime")));
static std::list<DbType_ptr> tmp = {
    DbType_ptr(new DbType(&DbType::mysql)),
    DbType_ptr(new DbType(&DbType::oracle)),
    DbType_ptr(new DbType(&DbType::presto)),
    DbType_ptr(new DbType(&DbType::trino)),
    DbType_ptr(new DbType(&DbType::postgresql)),
    DbType_ptr(new DbType(&DbType::mariadb)),
    DbType_ptr(new DbType(&DbType::tidb))};

long SQLDateTimeExpr::supportDbTypes = DbType::of(std::make_shared<std::list<DbType_ptr>>(tmp));

SQLDateTimeExpr::SQLDateTimeExpr()
{
}

SQLDateTimeExpr::SQLDateTimeExpr(long now, struct timezone *timeZone)
{
  setLiteral(now, timeZone);
}

void SQLDateTimeExpr::setLiteral(long x, struct timezone *timeZone)
{
  // if (x == null) {
  //     this->literal = null;
  //     return;
  // }

  // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  // if (timeZone != null) {
  //     format.setTimeZone(timeZone);
  // }
  // string_ptr  text = format.format(x);
  setLiteral(make_string_ptr(std::to_string(x)));
}

SQLDateTimeExpr::SQLDateTimeExpr(string_ptr literal)
{
  this->setLiteral(literal);
}

SQLExpr_ptr SQLDateTimeExpr::getLiteral()
{
  return literal;
}

void SQLDateTimeExpr::setLiteral(string_ptr literal)
{
  // SQLExpr_ptr tmp = std::dynamic_pointer_cast<SQLExpr>(std::make_shared<SQLCharExpr>(new SQLCharExpr(literal)));
  SQLExpr_ptr tmp = std::make_shared<SQLCharExpr>(literal);
  // SQLCharExpr_ptr tmp = std::make_shared<SQLCharExpr>(new SQLCharExpr(literal));
  setLiteral(tmp);
}

void SQLDateTimeExpr::setLiteral(SQLExpr_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLDateTimeExpr));
  }
  this->literal = x;
}

//@Override
SQLDataType_ptr SQLDateTimeExpr::computeDataType()
{
  return DATA_TYPE;
}

string_ptr SQLDateTimeExpr::getValue()
{
  if (instanceof <SQLExpr, SQLCharExpr>(literal.get()))
  {
    return (std::dynamic_pointer_cast<SQLCharExpr>(literal)->getText());
  }
  return make_string_ptr("");
}

//@Override
BOOL_ptr SQLDateTimeExpr::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (this->literal == expr)
  {
    setLiteral(target);
    return BOOL::TRUE;
  }
  return BOOL::FALSE;
}

//@Override
void SQLDateTimeExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLDateTimeExpr));
  visitor->endVisit(SharedObject(SQLDateTimeExpr));
}

//@Override
int SQLDateTimeExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  result = prime * result + ((literal == nullptr) ? 0 : literal->hashCode());
  return result;
}

//@Override
bool SQLDateTimeExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == nullptr)
  {
    return false;
  }
  if (getClass() != obj->getClass())
  {
    return false;
  }
  std::shared_ptr<SQLDateTimeExpr> other = std::dynamic_pointer_cast<SQLDateTimeExpr>(obj);
  if (literal == nullptr)
  {
    if (other->literal != nullptr)
    {
      return false;
    }
  }
  else if (!literal->equals(other->literal))
  {
    return false;
  }
  return true;
}

SQLObject_ptr SQLDateTimeExpr::clone()
{
  // SQLDateTimeExpr x = new SQLDateTimeExpr();
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLDateTimeExpr>(new SQLDateTimeExpr());
  std::shared_ptr<SQLDateTimeExpr> x = std::dynamic_pointer_cast<SQLDateTimeExpr>(clone_tmp);

  if (this->literal != nullptr)
  {
    x->setLiteral(std::dynamic_pointer_cast<SQLExpr>(literal->clone()));
  }

  return x;
}

//@Override
SQLObject_list_ptr SQLDateTimeExpr::getChildren()
{
  SQLObject_list_ptr tmp = std::make_shared<std::list<SQLObject_ptr>>();
  return tmp;
}

BOOL_ptr SQLDateTimeExpr::isSupport(DbType dbType)
{
  return ((dbType.mask & supportDbTypes) != 0)?BOOL::TRUE:BOOL::FALSE;
}

BOOL_ptr SQLDateTimeExpr::check(string_ptr str)
{
  if (str->empty() || str->length() != 8)
  {
    return BOOL::FALSE;
  }

  if (str->at(2) != ':' && str->at(5) != ':')
  {
    return BOOL::FALSE;
  }

  char c0 = str->at(0);
  char c1 = str->at(1);
  char c3 = str->at(3);
  char c4 = str->at(4);
  char c6 = str->at(6);
  char c7 = str->at(7);

  if (c0 < '0' || c0 > '9')
  {
    return BOOL::FALSE;
  }
  if (c1 < '0' || c1 > '9')
  {
    return BOOL::FALSE;
  }
  if (c3 < '0' || c3 > '9')
  {
    return BOOL::FALSE;
  }
  if (c4 < '0' || c4 > '9')
  {
    return BOOL::FALSE;
  }
  if (c6 < '0' || c6 > '9')
  {
    return BOOL::FALSE;
  }
  if (c7 < '0' || c7 > '9')
  {
    return BOOL::FALSE;
  }

  int HH = (c0 - '0') * 10 + (c1 - '0');
  int mm = (c3 - '0') * 10 + (c4 - '0');
  int ss = (c6 - '0') * 10 + (c7 - '0');

  if (HH > 24 || mm > 60 || ss > 60)
  {
    return BOOL::FALSE;
  }

  return BOOL::TRUE;
}
